package com.fw.ui.view.settings.usercontrol.parentmerchant.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.parentmerchant.bean.ParentMerchantBean;
import com.fw.service.parentmerchant.bean.SearchParentMerchantBean;
import com.fw.ui.bao.ParentMerchantBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.event.AbstractSortTableButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class SearchParentMerchantTableSortButtonClickListener extends AbstractSortTableButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private ParentMerchantBAO parentMerchantBAO;
    @Autowired
    private PropertyBAO propertyBAO;

    @SuppressWarnings("rawtypes")
    @Override
    public void click(ClickEvent event, int page, int size, Map<String, String> sortMap) throws Exception {
	SearchParentMerchantView searchParentMerchantView = (SearchParentMerchantView) this.getBasedComponent();
	SearchCriteriaBean<SearchParentMerchantBean> searchCriteriaBean = searchParentMerchantView.getSearchCriteriaBean();

	if (!sortMap.isEmpty()) {
	    searchCriteriaBean.setSortingCriteria(sortMap);
	} else {
	    String sortingDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.merchantuser.SearchMerchantUserView.default.table.sorting");
	    searchCriteriaBean.setDirection1(SearchCriteriaBean.DIRECTION_DESC);
	    searchCriteriaBean.setOrder1(sortingDefault);
	}

	searchCriteriaBean.setPage(page);
	searchCriteriaBean.setSize(size);
	SearchResultBean<ParentMerchantBean> searchResultBean = parentMerchantBAO.selectParentMerchant(searchCriteriaBean);

	Integer totalPage = searchResultBean.getTotalPages();
	if (totalPage == null) {
	    totalPage = 1;
	}
	Long totalElement = searchResultBean.getTotalElements();
	if (totalElement == null) {
	    totalElement = 0l;
	}
	List<BeanItem> beanItems = searchParentMerchantView.generateTableItemBean(searchResultBean);

	searchParentMerchantView.getTablePanel().clearTableData();
	searchParentMerchantView.getTablePanel().setManualPageTable(1);
	searchParentMerchantView.getTablePanel().setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);
    }

    @Override
    public boolean validate(ClickEvent event, int page, int size, Map<String, String> sortMap) {
	return true;
    }

}
